//
//  EWEUrl.swift
//  EWEMDT3
//
//  Created by 来 on 2019/11/19.
//  Copyright © 2019 laiguofeng. All rights reserved.
//

import Foundation

class EWEUrl {
    
    /// 在 HJ.plist 填入下面名字来配置环境
    /// 生产
    /// 演示
    /// 测试
    /// 开发
    /// 本地
    public static func SERVER_NAME() -> String {
        let pathStr = Bundle.main.path(forResource: "HJ.plist", ofType: nil)
        let HJ = NSDictionary.init(contentsOfFile: pathStr!) as? [String: Any]
        let value = (HJ?["HJ"] as? String) ?? ""
        if value.contains("本地") {
            return "BD"
        } else {
            return value
        }
    }
    
    /// 本地化部署时有效 （HJ.plist 配置 本地）
    public static let BD_URL: String = "https://dev.auscult.cn:9181/api/rest/v2/"
     
    public static let File_URL: String = "https://dev.auscult.cn/v2/files/"
    
    /// 单例
    public static let lgf = EWEUrl()
    private init() {
        if EWEUrl.isZJ() {
            return
        }
        let root = EWEUrl.getURLInfo()
        let serverName: String = (root["serverName"] as? String)!
        self.serverName = serverName
        dict = root[serverName] as? [String: Any]
    }
    public var serverName: String?
    private var dict: [String: Any]?
    private static func getURLInfo() -> [String: Any] {
        let urlInfo: [String: Any] = [
            "serverName": SERVER_NAME(),
            "BD": [
                "AES": true,
                "domain": BD_URL,
                "url": BD_URL,
                "baseUrl": BD_URL,
                "fileUrl": BD_URL + "file"
            ],
        ]
        return urlInfo
    }
    
    public var AES: Bool {
        get {
            if let dom = dict?["AES"] as? Bool {
                return dom
            } else {
                SVProgressHUD.showError("请检查 serverName 是否正确")
                return false
            }
        }
    }
    
    public var domain: String {
        get {
            if let dom = dict?["domain"] as? String {
                return dom
            } else {
                SVProgressHUD.showError("请检查 serverName 是否正确")
                return ""
            }
        }
    }
    
    public var url: String {
        get {
            if let ur = dict?["url"] as? String {
                return ur
            } else {
                SVProgressHUD.showError("请检查 serverName 是否正确")
                return ""
            }
        }
    }
    
    public var baseUrl: String {
        get {
            if let baseUr = dict?["baseUrl"] as? String {
                return baseUr
            } else {
                SVProgressHUD.showError("请检查 serverName 是否正确")
                return ""
            }
        }
    }
    
    public var fileUrl: String {
        get {
            if let fileUr = dict?["fileUrl"] as? String {
                return fileUr
            } else {
                SVProgressHUD.showError("请检查 serverName 是否正确")
                return ""
            }
        }
    }
    
    /// ***********   私人 host 部署时有效   ***********
    public static func isZJ() -> Bool {
        return SERVER_NAME() == "自己"
    }
    var ZJ_URL: String! {
        didSet {
            if ZJ_URL.count == 0 || !ZJ_URL.hasPrefix("http") {
                return
            }
            var new = ZJ_URL ?? ""
            new = new.replacingOccurrences(of: "!", with: "")
            new = new.replacingOccurrences(of: "！", with: "")
            var aes = false
            if ZJ_URL.hasSuffix("!") || ZJ_URL.hasSuffix("！") {
                aes = true
            }
            let root = EWEUrl.getZJURLInfo(new, aes)
            let serverName: String = (root["serverName"] as? String)!
            self.serverName = serverName
            dict = root[serverName] as? [String: Any]
        }
    }
    private static func getZJURLInfo(_ ZJ_URL: String, _ aes: Bool) -> [String: Any] {
        let urlInfo: [String: Any] = [
            "serverName": SERVER_NAME(),
            "自己": [
                "AES": aes,
                "domain": ZJ_URL + "/mobile",
                "url": ZJ_URL,
                "baseUrl": ZJ_URL + "router_rest",
                "fileUrl": ZJ_URL + "router_rest"
            ],
            ]
        return urlInfo
    }
}

/// ***********   私人 host 部署时有效   ***********
class customHost: BaseVC {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        let alertController = UIAlertController(title: "自定义环境(非测试/演示/生产)", message: "https://t1.hidoctor.wiki！(格式: host + ！), 末尾感叹号决定是否开启加密(感叹号支持中英文)", preferredStyle: .alert)
        alertController.addTextField { (textField: UITextField!) -> Void in
            textField.placeholder = "host"
            let customHJ = UserDefaults.standard.string(forKey: "LGFCUSTOMHOST") ?? ""
            textField.text = customHJ
        }
        let okAction = UIAlertAction(title: "确定", style: .default, handler: { action in
            let login = alertController.textFields!.first
            UserDefaults.standard.set(login?.text ?? "", forKey: "LGFCUSTOMHOST")
            if !(login?.text?.lgf_IsBlank())! {
                EWEUrl.lgf.ZJ_URL = login?.text ?? ""
            }
        })
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
    }
}
